package com.longtop.framework.sys.user.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.longtop.framework.base.BaseAction;
import com.longtop.framework.security.BaseCheckedException;
import com.longtop.framework.security.SessionObject;
import com.longtop.framework.sys.file.context.FileContext;
import com.longtop.framework.sys.team.model.TSysOrgTeam;
import com.longtop.framework.sys.team.model.TSysUserTeam;
import com.longtop.framework.sys.team.service.TeamService;
import com.longtop.framework.sys.user.model.TSysUser;
import com.longtop.framework.sys.user.service.UserService;
import com.longtop.framework.util.WebUtil;
import com.longtop.intelliweb.action.IActionContext;
import com.longtop.intelliweb.action.IInitContext;
import com.longtop.intelliweb.view.Dataset;

/**
 * 
 * @author 胡砥峰
 * 
 */
public class UserListAction extends BaseAction {

	private final static String DATASET_USER = "Dataset_User";
	private final String DATASET_TEAMUSER = "Dataset_TeamUser";
	private final String DATASET_TEAMORG = "Dataset_TeamOrg";

	/**
	 * 系统共用：初始化调用
	 */
	public void init(IInitContext context) {
		super.init(context);
	}

	/**
	 * 本页jsp：初始化业务数据(覆写父类方法)
	 */
	public void thisPageInit(IInitContext context) {
		// 调用公用查询方法
		commonQuery(context, false);

		// 加载员工/机构群组：
		Dataset teamDataset = this.getDataset(context, DATASET_TEAMUSER);
		if (!WebUtil.validateDatasetExist(this, teamDataset, DATASET_TEAMUSER)) {
			return;
		}
		Dataset teamOrgDataset = this.getDataset(context, DATASET_TEAMORG);
		if (!WebUtil.validateDatasetExist(this, teamOrgDataset, DATASET_TEAMORG)) {
			return;
		}
		// 读取员工群组主表：权限控制
		TeamService teamService = this.getBean(TeamService.BEAN_ID);
		SessionObject sessionObject = this.getSessionObject(context);
		teamService.getTeamByAuth(sessionObject, teamDataset, TSysUserTeam.class);

		// 读取机构群组主表：权限控制
		teamService.getTeamByAuth(sessionObject, teamOrgDataset, TSysOrgTeam.class);
	}

	/**
	 * 自定义查询
	 * 
	 * @param context
	 */
	public void searchUser(IActionContext context) {

		// 调用公用查询方法
		commonQuery(context, true);
	}

	/**
	 * 执行翻页查询
	 * 
	 * @param context
	 */
	public void loadUser(IActionContext context) {

		// 调用公用查询方法
		commonQuery(context, false);
	}

	/**
	 * 保存、修改、删除一起做！
	 * 
	 * @param context
	 */
	public void saveUser(IActionContext context) {

		Dataset dataset = context.getDataset(DATASET_USER);
		if (!WebUtil.validateDatasetChange(this, dataset, DATASET_USER)) {
			return;
		}

		UserService userService = this.getBean(UserService.BEAN_ID); // 调用Spring的业务逻辑类

		try {
			// 把dataset转成要删除的主键列表、新增bean及更新bean列表
			Map changeMap = WebUtil.datasetToObjectList(dataset, "userId", TSysUser.class);
			SessionObject sessionObject = this.getSessionObject(context);

			Integer[] count = userService.saveUser(changeMap, sessionObject);

			this.hintInfoMessage(WebUtil.getSubmitSuccessInfo(count[0].intValue(), count[1].intValue(), count[2].intValue()));
		} catch (BaseCheckedException e) {
			this.hintErrorMessage(e.getMessage()); // 在界面显示错误信息
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 通用查询.将数据查询出来，拼装数据。
	 * 
	 * @param object
	 *            上下文：IInitContext或IActionContext
	 * @param isFirstPageNoo
	 *            是否从第一页开始查询！
	 */
	private void commonQuery(Object context, boolean isFirstPageNo) {
		UserService userService = getBean(UserService.BEAN_ID);

		Dataset dataset = this.getDataset(context, DATASET_USER);
		if (!WebUtil.validateDatasetExist(this, dataset, DATASET_USER)) {
			return;
		}

		Map hashMap = new HashMap(); // ibatis查询的参数条件

		// 判断是否是由 init方法 调用
		if (context instanceof IActionContext) {
			// 取得界面传入的查询条件()
			hashMap = this.getElementsToMap(context);
		} else {
			IInitContext initContext = (IInitContext) context;
			// 根据用户数据权限，设置默认机构值：权限控制
			String orgId = userService.getOrgIdByAuth(initContext);

			hashMap.put("orgId", orgId);
		}

		// 当前第几页
		int pageNo = 1;
		if (!isFirstPageNo) {
			pageNo = dataset.getPageNo();
		}

		userService.queryForPageByIbatis("sysUserSql.selectUserList", null, hashMap, dataset, false);
	}

	/**
	 * 删除用户
	 * 
	 * @param context
	 */
	public void deleteUser(IActionContext context) {
		// 得到参数用户Id
		String userId = context.getCommandParameterValue("userId");
		// 调用Spring的业务逻辑类
		UserService userService = this.getBean(UserService.BEAN_ID);

		try {
			userService.deleteUser(userId);
		} catch (BaseCheckedException e) {
			this.hintErrorMessage(e.getMessage());
		}
	}

	/**
	 * 导出用户详情
	 * 
	 * @param context
	 */
	public void exportUser(IActionContext context) {
		UserService userService = this.getBean(UserService.BEAN_ID); // 调用Spring的业务逻辑类
		int exportMaxSize = FileContext.getFileContext().getMaxExportSize();// 获取导出最大允许数量

		Map queryMap = new HashMap();
		// 取得界面传入的查询条件()
		queryMap = this.getElementsToMap(context);

		List<Map<String, Object>> list = userService.queryForListByIbatis("sysUserSql.selectUserList", queryMap, 0, exportMaxSize);

		try {
			userService.exportUser(list, context);
		} catch (Exception e) {
			this.hintErrorMessage(e.getMessage());
			e.printStackTrace();
		}
	}
}